package applicationPlugin;

import interfacesplugins.InterfaceExplorateurJtree;
import interfacesplugins.InterfaceFilesTraitement;
import interfacesplugins.InterfacePlugins;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.TreePath;

public class ExplorateurJtree extends JFrame implements 
		InterfaceExplorateurJtree {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File file;
	private JScrollPane jsp = new JScrollPane();
	private JPanel explorer, container, barre;
	private JScrollPane sp;
	boolean etat;
	int i;
	private TreeSelectionEvent ev;
	private ArrayList<String> listeFiles = new ArrayList<String>();
	File dernierDossier;
	String extensionFichier;
	public ExplorateurJtree() {
		etat=true;
		explorer = new JPanel();
		container = new JPanel(new BorderLayout());
		barre = new JPanel();
		explorer.setVisible(true);
		sp = new JScrollPane();
		sp.getViewport().setPreferredSize(new Dimension(100, 200));
		explorer.setPreferredSize(new Dimension(100, 200));
	}

	

	public File setFile() {
		return file;
	}

	@Override
	public JScrollPane buildExplorer(final File f,
			final ArrayList<InterfaceFilesTraitement> listePlugin){
		dernierDossier = f;
		barre.removeAll();
		final JButton retour = new JButton("Retour");
		final JTextArea display = new JTextArea(f.getAbsolutePath());
		barre.add(retour);
		barre.add(display);
		container.add(explorer);
		container.add(barre, BorderLayout.NORTH);
		explorer.removeAll();
		retour.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				buildExplorer(f.getParentFile(), listePlugin);
			}
		});
		if (f.getParentFile()== null) {
			retour.setEnabled(false);
		} else {
			retour.setEnabled(true);
		}
			if (f.isDirectory()) {
				File[] fi = f.listFiles();
				JButton[] bouton = new JButton[fi.length];
				for (i = 0; i < f.listFiles().length; i++) {
					final File file = fi[i];
					Dimension d = new Dimension(80, 40);

					if (file.isDirectory()) {

						bouton[i] = new JButton(file.getName());
						bouton[i].setIcon(FileSystemView.getFileSystemView()
								.getSystemIcon(file));
						bouton[i].setPreferredSize(d);
						explorer.add(bouton[i]);
						bouton[i].addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {
								explorer.removeAll();

								buildExplorer(file, listePlugin);

								explorer.setVisible(false);
								explorer.revalidate();
								explorer.setVisible(true);

							}
						});

					} else {
						if (file.isFile()) {
							
							bouton[i] = new JButton(file.getName());
							bouton[i].setPreferredSize(d);
							bouton[i].setIcon(FileSystemView
									.getFileSystemView().getSystemIcon(file));
							explorer.add(bouton[i]);
							bouton[i].addActionListener(new ActionListener() {
								
								@Override
								public void actionPerformed(ActionEvent e) {
							
								
									int index = file.getName().lastIndexOf(".");
									
									extensionFichier = (file.getName().substring(index + 1, file.getName()
											.length()));
									extensionFichier = extensionFichier.toLowerCase();
									
									for (int j = 0; j < listePlugin.size(); j++) {
										for (int k = 0; k < listePlugin.get(j).getExtension().length; k++) {
											if (extensionFichier.equals(listePlugin.get(j)
													.getExtension()[k])) {
												
												listePlugin.get(j).lancerFichier(file).addWindowListener(new WindowAdapter(){
													  public void windowClosing(WindowEvent we){
														  listeFiles.remove(listeFiles.indexOf(file.getAbsolutePath()));
													  }
												});
												
												listeFiles.add(file.getAbsolutePath());

											}
										}
									}

									

								}
							});

						}
					}

				}
			
		}
		Dimension d = new Dimension(100, 800);
		explorer.setPreferredSize(d);
		explorer.revalidate();
		explorer.setVisible(true);
		sp.getViewport().add(container);
		sp.revalidate();

		return sp;
	}

	@Override
	public String getVersion() {
		return "1.0";
	}
	
	
	@Override
	public String getNomPlugin() {
		// TODO Auto-generated method stub
		return "JTree Explorer";
	}

	@Override
	public String getDeveloppeur() {
		// TODO Auto-generated method stub
		return "EL MALLEM AMINE";
	}

	@Override
	public String[] getExtension() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public File getLastFile() {
		return dernierDossier;
	}

	public ArrayList<String> getFichierOuvert() {
		return listeFiles;
	}



	@Override
	public boolean getEtat() {
		// TODO Auto-generated method stub
		return false;
	}



	@Override
	public boolean setEtat(boolean etat) {
		// TODO Auto-generated method stub
		this.etat=etat;
		return etat;
	}

	
	}
